package cn.px.rest.controller.purchase;

import cn.px.common.util.R;
import cn.px.dao.dto.purchase.SupplierGoodsRecordDTO;
import cn.px.dao.entity.purchase.SupplierGoodsRecord;
import cn.px.service.purchase.SupplierGoodsRecordService;
import cn.px.dao.util.ExcelUtil;
import cn.px.dao.vo.export.purchase.ExportSupplierGoodsRecordVO;
import cn.px.dao.vo.query.purchase.SupplierGoodsRecordVO;
import cn.px.common.util.BeanConverterUtil;
import cn.px.common.validation.group.AddGroup;
import cn.px.common.validation.group.UpdateGroup;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import lombok.AllArgsConstructor;
import cn.px.common.bean.AbstractPageResultBean;
import cn.px.rest.log.annotation.SysLog;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description 供货记录 控制器
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2020,北京乾元坤和科技有限公司 All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("/purchase/supplier-goods-records")
public class SupplierGoodsRecordController {

  private final  SupplierGoodsRecordService supplierGoodsRecordService;

  /**
   * 通过id查询单条记录
   *
   * @param id 主键
   * @return R
   */
  @GetMapping("/{id}")
  public R<SupplierGoodsRecord> getById(@PathVariable("id") String id){
      return new R<>(supplierGoodsRecordService.getModelById(id));
  }

  /**
   * 新增记录
   *
   * @param supplierGoodsRecordDTO 供货记录DTO
   * @return R
   */
  @SysLog("新增供货记录")
  @PostMapping
  @PreAuthorize("@pms.hasPermission('purchase-supplier-goods-records-add')")
  public R save(@Validated({AddGroup.class}) @RequestBody SupplierGoodsRecordDTO supplierGoodsRecordDTO) {
      return new R<>(supplierGoodsRecordService.saveModelByDTO(supplierGoodsRecordDTO));
  }

  /**
   * 修改记录
   *
   * @param supplierGoodsRecordDTO 供货记录DTO
   * @return R
   */
  @SysLog("修改供货记录")
  @PutMapping("/{id}")
  @PreAuthorize("@pms.hasPermission('purchase-supplier-goods-records-edit')")
  public R update(@PathVariable("id") String id, @Validated({UpdateGroup.class}) @RequestBody SupplierGoodsRecordDTO supplierGoodsRecordDTO) {
      supplierGoodsRecordService.modModelByDTO(supplierGoodsRecordDTO);
      return new R<>();
  }

  /**
   * 通过id删除一条记录
   *
   * @param id 主键
   * @return R
   */
  @SysLog("删除供货记录")
  @DeleteMapping("/{id}")
  @PreAuthorize("@pms.hasPermission('purchase-supplier-goods-records-del')")
  public R removeById(@PathVariable String id){
    supplierGoodsRecordService.deleteByPrimaryKey(id);
    return new R<>();
  }

    /**
     * 批量记录
     *
     * @param ids 主键
     * @return R
     */
    @SysLog("批量删除供货记录")
    @DeleteMapping("")
    @PreAuthorize("@pms.hasPermission('purchase-supplier-goods-records-del')")
    public R removeIds(@RequestBody List<String> ids){
        supplierGoodsRecordService.batchDelete(ids);
        return new R<>();
    }

  /**
   * 获取分页
   *
   * @param pageNum 当前页码
   * @param pageSize 每页条数
   * @param supplierGoodsRecordDTO 供货记录DTO
   * @return R
   */
  @GetMapping("/page")
  public R<AbstractPageResultBean<SupplierGoodsRecordVO>> query(SupplierGoodsRecordDTO supplierGoodsRecordDTO, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "20") int pageSize) {
      return new R<>(supplierGoodsRecordService.selectPageList(supplierGoodsRecordDTO, pageNum, pageSize));
  }

  /**
   * 获取列表
   *
   * @param supplierGoodsRecordDTO 供货记录DTO
   * @return R
   */
  @GetMapping("")
  public R query(SupplierGoodsRecordDTO supplierGoodsRecordDTO) {
      return new R<>(supplierGoodsRecordService.getModelListByDTO(supplierGoodsRecordDTO));
  }

  /**
     * 供货记录导出
     *
     * @param supplierGoodsRecordDTO 供货记录DTO
     * @return R
     */
  @GetMapping("/export")
  @SysLog("供货记录导出")
  @PreAuthorize("@pms.hasPermission('purchase-supplier-goods-records-export')")
  public void export(HttpServletResponse response, SupplierGoodsRecordDTO supplierGoodsRecordDTO) {
    List<SupplierGoodsRecord> list = supplierGoodsRecordService.getModelListByDTO(supplierGoodsRecordDTO);
    ExcelUtil.exportResponseDict(response, ExportSupplierGoodsRecordVO.class, BeanConverterUtil.copyListProperties(list, ExportSupplierGoodsRecordVO.class), "供货记录");
  }

}
